<?php

namespace app\teacher\controller;

use app\teacher\model\Thesis as ThesisModel;
use app\teacher\security\TchCheck as TchCheckLogic;
use app\teacher\security\Auth as AuthLogic;
use app\teacher\model\AddThesis as WriteThesisModel;
use think\Controller;
use think\Session;

class TisKey extends Controller
{
    /**
     * 授权码管理页面跳转
     * @access public
     * @return mixed 返回登录视图
     */
    public function tisKey()
    {
        //检测教师登录情况
        $tchCheck = new TchCheckLogic();
        if ($tchCheck->check() == 1) {
            //检测是否有查看个人列表权限
            $auth = new AuthLogic();
            if ($auth->check('list', Session::get('token')['tchUsername'])) {
                $thesis = new ThesisModel();
                //定义需要查询的字段
                $field = 'tisId,tisTitle,tisKey';
                $result = $thesis::where('tisAuthor',Session::get('token')['tchUsername'])->field($field)->select();
                if ($result !== false) {
                    $this->assign('thesisList', $result);
                } else {
                    return "查询出错，请稍后再试";
                }
            } else {
                return "错误500：您没有添加题目的权限";
            }
        } else {
            return "错误600：您的登录信息出现问题，请重新登录"
            . "<br>" . "1.您的帐号在其他地方登录"
            . "<br>" . "2.您在线时间过长（5小时以上）"
            . "<br>" . "3.令牌验证错误";
        }
        return $this->fetch('tisKey/tisKey');
    }

    /**
     * 授权码管理页面跳转
     * @access public
     * @param string $tisId 需要修改授权码的题目编号
     * @param string $key 新的授权码
     * @return mixed 返回执行结果
     */
    public function do_tisKey($tisId='',$tisKey=''){
        //检测教师登录情况
        $tchCheck = new TchCheckLogic();
        if ($tchCheck->check() == 1) {
            //检测是否有查看个人添加权限
            $auth = new AuthLogic();
            $username=Session::get('token')['tchUsername'];
            if ($auth->check('add', $username)) {
                $thesis=new WriteThesisModel();
                if($result=$thesis::where('tisId',$tisId)->find()){
                    if($result['tisAuthor']==$username){
                        if($thesis->isUpdate(true)->save([
                            'tisId'=>$tisId,
                            'tisKey'=>$tisKey,
                            'UpdateTime'=>date('y-m-d h:i:s', time())
                        ])){
                            return 1;
                        }else{
                            return "修改失败，请稍后再试";
                        }
                    }else{
                        return "不能修改他人题目的授权码";
                    }
                }else{
                    return "修改授权码的题目不存在";
                }
            }else{
                return "错误500：您没有添加题目的权限";
            }
        }else{
            return "错误600：您的登录信息出现问题，请重新登录"
            . "<br>" . "1.您的帐号在其他地方登录"
            . "<br>" . "2.您在线时间过长（5小时以上）"
            . "<br>" . "3.令牌验证错误";
        }
    }
}